// miniprogram/pages/superModify/superModify.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    phone: "",
  },

  changeName: function (e) {
    this.setData({
      name: e.detail.value,
    })
  },

  changePhone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },

  addSuperUser: function () {
    let that = this
    wx.cloud.callFunction({
      name: "getUserByNameAndPhone",
      data: {
        name: that.data.name,
        phone: that.data.phone,
      },
      success: res => {
        console.log("根据name和phone调用云函数获取用户成功", res)
        let tempOpenId =  res.result.data[0].openId
        
        wx.cloud.callFunction({
          name: "addSuperUser",
          data: {
            openId: tempOpenId
          },
          success: sres => {
            console.log("调用云函数添加管理员成功", sres)
          },
          fail: sres => {
            console.log("调用云函数添加管理员失败", sres)
          }
        })
      },
      fail: res => {
        console.log("根据name和phone调用云函数获取用户失败", res)
      }
    })
  },

  removeSuperUser: function () {
    let that = this
    wx.cloud.callFunction({
      name: "getUserByNameAndPhone",
      data: {
        name: that.data.name,
        phone: that.data.phone,
      },
      success: res => {
        console.log("根据name和phone调用云函数获取用户成功", res)
        let tempOpenId = res.result.data[0].openId

        wx.cloud.callFunction({
          name: "removeSuperUser",
          data: {
            openId: tempOpenId
          },
          success: sres => {
            if(sres.result == false) {
              console.log("调用云函数删除管理员失败", sres)
              wx.showToast({
                image: "/images/unhappy.png",
                title: '查无此人',
              })
            } else {
              console.log("调用云函数删除管理员成功", sres)
            }
          },
          fail: sres => {
            console.log("调用云函数删除管理员失败", sres)
          }
        })
      },
      fail: res => {
        console.log("根据name和phone调用云函数获取用户失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})